#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion
using Neatly.UI;
using UnityEngine;
using UnityEditor;

namespace NeatlyEditor.UI
{
    public static class UICreateMenu
    {
        public const string TITLE = "GameObject/NeatlyUI/";

        [MenuItem(TITLE + "NImage", priority = 5001)]
        static void CreateImage()
        {
            NImage img = CreateObj<NImage>("bg", Selection.activeTransform);
            img.raycastTarget = false;
            UIUtility.SetImageSprite(img, "block");
        }

        [MenuItem(TITLE + "EmptyRaycast")]
        static void CreateEmptyRaycast()
        {
            EmptyRaycast emptyRay = CreateObj<EmptyRaycast>("EmptyRaycast", Selection.activeTransform);
            emptyRay.raycastTarget = true;
        }

        [MenuItem(TITLE + "EmptyButton")]
        static void CreateEmptyButton()
        {
            EmptyRaycast emptyRay = CreateObj<EmptyRaycast>("Btn", Selection.activeTransform);
            emptyRay.rectTransform.sizeDelta = new Vector2(160f, 30f);
            emptyRay.raycastTarget = true;
            var btn = emptyRay.gameObject.AddComponent<NButton>();
            btn.SetTransition(NButtonBase.Transition.None);
        }

        [MenuItem(TITLE + "NSlider")]
        static void CreateSlider()
        {
            NSlider slider = CreateObj<NSlider>("Sld", Selection.activeTransform);
            slider.rectTransform.sizeDelta = new Vector2(160f, 30f);
            NImage bg = CreateObj<NImage>("bg", slider.transform);
            bg.color = Color.white;
            SetAnchorFull(bg);
            RectTransform fillArea = CreateObj<RectTransform>("FillArea", slider.transform);
            SetAnchorFull(fillArea);
            NImage fillRect = CreateObj<NImage>("Fill", fillArea);
            fillRect.color = Color.green;
            SetAnchorFull(fillRect);
            slider.fillRect = fillRect.rectTransform;
            slider.SetValue(0.7f);
        }

        public static void SetAnchorFull(Component behaviour)
        {
            var rt = behaviour.transform as RectTransform;
            rt.anchorMin = Vector2.zero;
            rt.anchorMax = Vector2.one;
            rt.offsetMin = Vector2.zero;
            rt.offsetMax = Vector2.zero;
            rt.localPosition = Vector3.zero;
            rt.localScale = Vector3.one;
        }

        public static T CreateObj<T>(string name, Transform parent) where T : Component
        {
            GameObject obj = new GameObject();
            T component = obj.AddComponent<T>();
            obj.name = name;
            obj.transform.SetParent(parent);
            obj.transform.localScale = Vector3.one;
            obj.transform.localPosition = Vector3.zero;
            return component;
        }
    }
}